package com.zhiyume.api.controller;

import com.zhiyume.dao.api.TArticleMapper;
import com.zhiyume.dao.api.TCmsTemplateMapper;
import com.zhiyume.dao.entity.*;
import com.zhiyume.service.ArticleService;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.service.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author jiaxz
 * @descriton 文章相关接口
 * @date 2017-09-28
 */
@RestController
@RequestMapping("/article")
@ApiIgnore
public class ArticleAction {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private TArticleMapper tArticleMapper;

    @Autowired
    private TCmsTemplateMapper tCmsTemplateMapper;

    private String errType = ConstantEnum.CONSTANT_MISS_PARA.getErrType();

    private String errMsg = ConstantEnum.CONSTANT_MISS_PARA.getErrMsg();

    private String successType = ConstantEnum.CONSTANT_SUCCESS.getErrType();

    private String successMsg = ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
    /**
     * @param id
     * @return
     * @description 根据Id查询文章信息接口
     * add by jiaxz
     */
    @GetMapping("/findArticleById")
    public String findArticleById(long id) {
        if (0 == id) {
            return JsonUtil.returnErrorInfo(errType, errMsg);
        }
        return articleService.findArticleById(id);
    }

    /**
     * @description 根据标题查新文章信息接口
     * @param title
     * @return
     */
    @GetMapping("/findArticleByTitle")
    public String findArticleByTitle(String title) {
        if (StringUtil.isBlank(title)) {
            return JsonUtil.returnErrorInfo(errType, errMsg);
        }
        return articleService.findArticleByTitle(title);
    }

    /**
     * @description 添加文章
     * @param tArticle
     * @return
     */
    @RequestMapping("/addArticle")
    public String addArticle(TArticleWithBLOBs tArticle) {
        //TODO 判断参数是否合法，未知必填参数
        return articleService.addArticle(tArticle);
    }

    /**
     * @description 修改文章信息
     * @param tArticle
     * @return
     */
    @RequestMapping("/updateArticle")
    public String updateArticle(TArticleWithBLOBs tArticle) {
        return articleService.updateArticle(tArticle);
    }

    /**
     * @description 根据Id删除文章信息
     * @param id
     * @return
     */
    @RequestMapping("/deleteArticleById")
    public String deleteArticleById(long id){
        if (0 == id) {
            return JsonUtil.returnErrorInfo(errType, errMsg);
        }
        return articleService.deleteArticleById(id);
    }
    /**
     * @description 根据Id删除文章信息
     * @param title
     * @return
     */
    @RequestMapping("/deleteArticleByTitle")
    public String deleteArticleByName(String title){
        if (StringUtil.isBlank(title)) {
            return JsonUtil.returnErrorInfo(errType, errMsg);
        }
        return articleService.deleteArticleByTitle(title);
    }
}
